#!/bin/sh -e

# 使用前先修改文件权限 chmod 777 build-xcframework.sh
# ./build-xcframework.sh运行脚本

BUILD_SIMULATOR_INTER_VARIABLES="VALIDATE_WORKSPACE=NO MACH_O_TYPE=staticlib ONLY_ACTIVE_ARCH=NO CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= DEBUG_INFORMATION_FORMAT=dwarf"

BUILD_IPHONEOS_INTER_VARIABLES="VALIDATE_WORKSPACE=NO ONLY_ACTIVE_ARCH=NO CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= SKIP_INSTALL=YES GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=NO CLANG_ENABLE_CODE_COVERAGE=NO STRIP_INSTALLED_PRODUCT=NO MACH_O_TYPE=staticlib DEBUG_INFORMATION_FORMAT=dwarf"

WORKSPACE_NAME=""
function preBuildCheck() {
    # 获取当前目录下.xcworkspace的文件
    for file in *.xcworkspace; do
        WORKSPACE_NAME=$(basename "$file" .xcworkspace)
    done
    echo "$WORKSPACE_NAME"
    echo '====> 清理编译数据'
    echo '====> Command line invocation:'
    echo '====> rm -rf XCFramework/Build'
    # 删除编译文件夹
    rm -rf XCFramework/Build
    echo '====> 编译数据清理完成'
    echo
    echo '====> Clean编译环境'
    # clean一下Release编译环境
    xcodebuild clean -workspace $WORKSPACE_NAME.xcworkspace -scheme $WORKSPACE_NAME -configuration Release
    echo '====> 编译环境Clean完成'
    echo
}

function startBuild() {
    # 编译模拟器的Release环境
    # 指定编译后的framework为静态包
    # 输出目录为 XCFramework/Build/Simulator
    echo '====> 开始编译模拟器Release'
    xcodebuild build $BUILD_SIMULATOR_INTER_VARIABLES \
           -workspace $WORKSPACE_NAME.xcworkspace \
           -scheme $WORKSPACE_NAME \
           -configuration Release \
           -derivedDataPath XCFramework/Build/Simulator \
           -sdk iphonesimulator \
           -destination-timeout 3
    echo '====> 编译模拟器Release完成'
    echo
    
    # 编译真机的Release环境
    # 指定编译后的framework为静态包
    # 输出目录为 XCFramework/Build/Device
    echo '====> 开始编译真机Release'
    xcodebuild archive $BUILD_IPHONEOS_INTER_VARIABLES \
            -scheme $WORKSPACE_NAME \
            -configuration Release \
            -workspace $WORKSPACE_NAME.xcworkspace \
            -derivedDataPath XCFramework/Build/Device \
            -sdk iphoneos \
            -archivePath XCFramework/Build/Device/$WORKSPACE_NAME
    echo '====> 编译真机Release完成'
    echo
}

function createXCFramework() {
    echo '====> 开始移除xcframework包'
    echo '====> Command line invocation:'
    echo '====> rm -rf XCFramework/$WORKSPACE_NAME.xcframework'
    # 移除原来的xcframework包
    rm -rf XCFramework/$WORKSPACE_NAME.xcframework
    echo '====> xcframework包移除完成'
    echo
    # 合并Release的模拟器和真机framework

    echo '====> 开始合并xcframework'
    xcodebuild -create-xcframework \
        -allow-internal-distribution \
        -framework XCFramework/Build/Device/Build/Intermediates.noindex/ArchiveIntermediates/$WORKSPACE_NAME/IntermediateBuildFilesPath/UninstalledProducts/iphoneos/$WORKSPACE_NAME.framework \
        -framework XCFramework/Build/Simulator/Build/Products/Release-iphonesimulator/$WORKSPACE_NAME/$WORKSPACE_NAME.framework \
        -output XCFramework/$WORKSPACE_NAME.xcframework
    echo '====> xcframework合并完成'
    echo
}

function dealBuildFile() {
    echo '====> 合并完成后清理编译数据'
    echo '====> Command line invocation:'
    echo '====> rm -rf XCFramework/Build'
    # 删除编译文件夹
    rm -rf XCFramework/Build
    echo '====> 编译数据清理完成'
}

START_SHELL_TIME=`date +%s`

preBuildCheck

startBuild

createXCFramework

dealBuildFile


END_SHELL_TIME=`date +%s`

SHEL_RUN_TIME=$((END_SHELL_TIME-START_SHELL_TIME))
echo "** 脚本运行时间为：$SHEL_RUN_TIME 秒 **"
